<?php
	//单页面操作类
namespace Admin\Controller;
use Org\Yike\Tree;
class OneController extends CommonController {
    public function _initialize() {
		parent::_initialize();
        //import('ORG.Yike.Tree');
        $this->one= D('one');
        $this->onecate = D('one_cate');
    }
	
	//单页面列表
    public function index() {
        $prefix = C('DB_PREFIX');
        $sort = I("request.sort", 'ordid');
        $order = I("request.order",'DESC');
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
       $sql = "SELECT id,pid,name FROM ".$prefix."one_cate as oc LEFT JOIN ".$prefix."one as o on o.cate_id =oc.id WHERE oc.site_id = ".$this->siteId." ORDER BY ordid DESC";
        $result = $this->onecate->query($sql);
       // echo $this->onecate->getLastSql();
        $array = array();
        foreach($result as $r) {
            //是否有下一级
            if ($this->onecate->where(array('pid'=>$r['id']))->count('id')) {
                $r['str_manage'] = '';
            } else {
                $r['str_manage'] = '<a href="'.U('One/edit', array('cate_id'=>$r['id'])).'">'.L('edit').'</a>';
            }
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'>\$id</td>
                <td>\$spacer\$name</td>
                <td align='center'>\$str_manage</td>
                </tr>";
       // var_dump($array);
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        $this->assign('list_table', true);
        $this->display();
    }
    protected function _before_insert($data) {
        return $this->_insertPic($data);
    }
	//单页面编辑
    public function edit() {
        $one = D('one');
        if (IS_POST){
            if (false === $data = $one->create()) {
                $this->error($one->getError());
            }
            if (!$one->where(array('cate_id'=>$data['cate_id']))->count()) {
                $data['cate_alias'] = getCateAlias($data['cate_id'],'OneCate');
                $data = $this->_insertPic($data);
                $one->add($data);
            } else {
				$data=$this->_updatePic($data,'cate_id');
                $one->save($data);
            }
            $this->success(L('operation_success'), U('One/index'));
        } else {
            $cate_id = I('cate_id','intval');
            $cate_info = $this->onecate->field('id,name')->where(array('id'=>$cate_id))->find();
            !$cate_info && $this->redirect('One/index');
            $this->assign('cate_info', $cate_info);
            $info = $one->where(array('cate_id'=>$cate_id))->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

}